package com.sgb.kjwl.view.ui.activity.leaseAsk;

import android.content.Context;
import android.content.Intent;


import android.graphics.drawable.Drawable;

import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;


import androidx.databinding.DataBindingUtil;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityUpdateLeasePriceBinding;
import com.sgb.kjwl.di.machine.DaggerMachineComponent;
import com.sgb.kjwl.di.machine.MachineModule;
import com.sgb.kjwl.model.entity.user.UnitEntity;
import com.swgk.core.dialog.RoundCornerDialog;
import com.sgb.kjwl.viewmodel.machine.UpdateLeaseQuePriceModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.util.MToast;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

/**
 * 报价页面(修改报价界面已和一键报价界面合并,此界面暂时废弃)
 */
@Deprecated
public class UpdateLeaseQuedPriceActivity extends AppActivity {

    @Inject
    UpdateLeaseQuePriceModel viewModel;

    private ActivityUpdateLeasePriceBinding binding;
    private String id = "";
    private String type = "";
    private List<UnitEntity> unitList = new ArrayList<UnitEntity>();
    private String key = "";
    private String value = "";
    private String updateTime;
    private String negotiable;
    private boolean isPriceUnit = false;//是否可随意选择价格单位

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerMachineComponent.builder().appComponent(appComponent)
                .machineModule(new MachineModule(this))
                .build()
                .inject(this);
    }

    public static void start(Context context, String id, String type, String quoteUtil, String negotiable) {
        Intent intent = new Intent();
        intent.setClass(context, UpdateLeaseQuedPriceActivity.class);
        intent.putExtra("id", id);
        intent.putExtra("type", type);
        intent.putExtra("quoteUtil", quoteUtil);
        intent.putExtra("negotiable", negotiable);
//        intent.putExtra("updateTime", updateTime);
        context.startActivity(intent);
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_update_lease_price);
        binding.include.ivBack.setOnClickListener(view -> finish());
        binding.include.titleView.setText("报价页面");
        binding.tvSubmit.setOnClickListener(v -> showDialogs());
        binding.tvPriceUnit.setOnClickListener(v -> {
            if (isPriceUnit) {
                showPriceUnitPop();
            }
        });
    }

    @Override
    protected void init() {
        id = getIntent().getStringExtra("id");
        type = getIntent().getStringExtra("type");
        value = getIntent().getStringExtra("quoteUtil");
        negotiable = getIntent().getStringExtra("negotiable");
        isPriceUnit = "1".equals(negotiable);
        setDrawable(isPriceUnit);

        if ("0".equals(type)) {//机械价格单位
            viewModel.getUnit();
        } else if ("1".equals(type)) {//设备价格单位
            viewModel.getRentpriceUnit();
        } else if ("2".equals(type)) {//车辆价格单位
            viewModel.getNeedLeaseCarPrice();
        } else if ("3".equals(type)) {//房产价格单位
            viewModel.getEstatePriceUnit();
        } else if ("4".equals(type)) {//土地价格单位
            viewModel.getPriceUnitModel();
        }

    }

    private void toSubmit() {
        if (TextUtils.isEmpty(binding.tvPrice.getText().toString())) {
            MToast.showToast(UpdateLeaseQuedPriceActivity.this, "请输入金额");
            return;
        }

        String edit = "";
        if (!TextUtils.isEmpty(binding.cfkEdit.getEditContent())) {
            edit = binding.cfkEdit.getEditContent();
        }
        viewModel.changePrice(id, binding.tvPrice.getText().toString(), edit, value, key, type);

    }

    //设置默认值
    public void doPriceUnit(List<UnitEntity> unitList) {
        this.unitList = unitList;
        if (unitList != null && unitList.size() > 0) {
            UnitEntity leaseCarPrice = getLeaseCarPriceByValue(unitList, value);
            if (leaseCarPrice == null) {
                if (TextUtils.isEmpty(value)) {
                    leaseCarPrice = unitList.get(0);
                    key = leaseCarPrice.getNo();
                    value = leaseCarPrice.getName();
                }
            } else {
                key = leaseCarPrice.getNo();
                value = leaseCarPrice.getName();
            }

            binding.tvPriceUnit.setText(value);
        }
    }

    /**
     * 根据value 找出在list中的实体
     *
     * @param unitList
     * @param v
     * @return
     */
    private UnitEntity getLeaseCarPriceByValue(List<UnitEntity> unitList, String v) {
        UnitEntity carPrice = null;
        for (int i = 0; i < unitList.size(); i++) {
            carPrice = unitList.get(i);
            if (carPrice.getName().equals(v)) {
                return carPrice;
            }
        }
        return null;
    }

    /**
     * 弹出报价单位
     */
    private void showPriceUnitPop() {
        if (unitList != null && unitList.size() > 0) {
            OptionsPickerView businessOption = new OptionsPickerBuilder(UpdateLeaseQuedPriceActivity.this, (options1, options2, options3, v) -> {
                key = unitList.get(options1).getNo();
                value = unitList.get(options1).getName();
                binding.tvPriceUnit.setText(value);
            }).build();
            businessOption.setPicker(unitList);
            businessOption.setSelectOptions(0);
            businessOption.setTitleText("选择单位");
            businessOption.show();
        }
    }

    /**
     * 确认提交提示对话框
     */
    private void showDialogs() {
        View view = View.inflate(UpdateLeaseQuedPriceActivity.this, R.layout.dialog_two_btn, null);
        final RoundCornerDialog roundCornerDialog = new RoundCornerDialog(UpdateLeaseQuedPriceActivity.this, 0, 0, view, R.style.RoundCornerDialog);
        roundCornerDialog.setCanceledOnTouchOutside(false);// 设置点击屏幕Dialog不消失

        TextView tv_message = (TextView) view.findViewById(R.id.tv_message);
        TextView tv_title = view.findViewById(R.id.tv_title);
        TextView tv_logout_confirm = (TextView) view.findViewById(R.id.tv_logout_confirm);
        tv_title.setVisibility(View.VISIBLE);
        TextView tv_logout_cancel = (TextView) view.findViewById(R.id.tv_logout_cancel);
        tv_title.setText("提交报价");
        tv_message.setText("确认提交此报价?");
        //确定
        tv_logout_confirm.setOnClickListener(v -> {
            roundCornerDialog.dismiss();
            toSubmit();
//            viewModel.judgeImValid(id, updateTime);
        });
        //取消
        tv_logout_cancel.setOnClickListener(v -> roundCornerDialog.dismiss());
        roundCornerDialog.show();
    }

//    public void toJudgeImValid() {
//        toSubmit();
//    }

    private void setDrawable(boolean b) {
        Drawable drawable = null;
        if (b) {
            drawable = getResources().getDrawable(R.mipmap.arrow_right);
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        }
        binding.tvPriceUnit.setCompoundDrawables(null, null, drawable, null);
    }
}
